
/**
 * @file    bt_flash.c
 * @author  chenwenrui
 * @brief   单片机EEPROM读写，保存设备参数
 * @version V1.0
 * @date 2023-12-21
 * @copyright Copyright (c) 2022
 *
 * 系统资源：
 *      EEPROM
 *
 */
#include "EEPROM.h"
#include "bt_flash.h"

// #define IAP_INIT_FLAG 0xaa55
// #define IAP_LOCK_ID_BIT6_ADDRESS (IAP_LOCK_BASE_ADDRESS + 2)
// #define IAP_LOCK_ID_BIT6_FLAG 0xcade

// EEPROM 地址  擦除会擦整个扇区  每个扇区512B  EEPROM地址从0开始
#define EEPROM_BASE_ADDRESS 0x0000

#define VERSION_FLAG_ADDRESS (0x0000)
#define VERSION_ADDRESS (0x0200)
#define MODE_ADDRESS (0x0400)
#define HIGHLOCK_ADDRESS (0x0600)
#define IAP_DEVADDR_ADDRESS (0x0800)
#define DISPLAY_MODE_ADDRESS (0x0a00)
#define DEV_SECRET_ADDRESS (0x0C00)

#define HIGH_LOCK_FLAG 0xaa55
#define VERSION_FLAG 0xaa55

static unsigned short g_flash_flag; // flash写标志
/**
 * @brief  保存设备高位地址，原枪锁大于128把锁，需要设置此位，已弃用
 * @note
 * @retval
 */
void bt_flash_write_board_highaddr(unsigned short value)
{
    EEPROM_write_n((unsigned int)HIGHLOCK_ADDRESS, (unsigned char *)&value, 2);
}

// void bt_flash_init(void)
//{

//}

/**
 * @brief  创建密码
 * @param  context：输入加密内容，4bytes
 * @param  secret : 返回密码数据，4bytes
 * @note
 * @retval
 */
void bt_flash_create_passwd(unsigned char *context, unsigned char *secret)
{
    unsigned char i = 0;
    unsigned char  local[4] = {'B', 'A', 'I', 'T'};
    for (i = 0; i < 4; i++)
    {
        secret[i] = context[i] ^ local[i];
    }
}
/**
 * @brief  读取密码
 * @param  secret : 返回密码数据，4bytes
 * @note
 * @retval
 */
void bt_flash_read_secret(unsigned char *secret)
{
    unsigned char  temp[4];
    EEPROM_read_n(DEV_SECRET_ADDRESS, (unsigned char *)&g_flash_flag, 2);
    if (g_flash_flag == 0xcade)
    {
        EEPROM_read_n((DEV_SECRET_ADDRESS + 2), (unsigned char *)&temp, 4);
        bt_flash_create_passwd(temp, secret);
    }
}

/**
 * @brief  保存密码
 * @param  secret : 要写入flash的密码，4bytes
 * @note
 * @retval
 */
void bt_flash_write_secret(unsigned char *secret)
{
    g_flash_flag = 0xcade;
    EEPROM_SectorErase((unsigned int)DEV_SECRET_ADDRESS);
    EEPROM_write_n((unsigned int)DEV_SECRET_ADDRESS, (unsigned char *)&g_flash_flag, 2);
    EEPROM_write_n((unsigned int)(DEV_SECRET_ADDRESS + 2), (unsigned char *)secret, 4);
}
/**
 * @brief  读取板卡地址
 * @param  
 * @note
 * @retval 板卡地址
 */
unsigned char bt_flash_read_board_addr(void)
{
    unsigned char addr = 0;
    EEPROM_read_n(IAP_DEVADDR_ADDRESS, (unsigned char *)&g_flash_flag, 2);
    if (g_flash_flag == 0xcade)
    {
        EEPROM_read_n((IAP_DEVADDR_ADDRESS + 2), (unsigned char *)&addr, 1);
    }
    else
        addr = 0xff;

    return addr;
}

/**
 * @brief  保存板卡地址
 * @param  addr : 板卡地址
 * @note
 * @retval
 */
void bt_flash_write_board_addr(unsigned char addr)
{
    g_flash_flag = 0xcade;
    EEPROM_SectorErase((unsigned int)IAP_DEVADDR_ADDRESS);
    EEPROM_write_n((unsigned int)IAP_DEVADDR_ADDRESS, (unsigned char *)&g_flash_flag, 2);
    EEPROM_write_n((unsigned int)(IAP_DEVADDR_ADDRESS + 2), &addr, 1);
}

/**
 * @brief  读取LCD显示方向
 * @param  
 * @note
 * @retval LCD的显示方向，0-正 1-反
 */
unsigned char bt_flash_read_display_dir(void)
{
    unsigned char dir = 0;
    EEPROM_read_n(DISPLAY_MODE_ADDRESS, (unsigned char *)&g_flash_flag, 2);
    if (g_flash_flag == 0xcade)
    {
        EEPROM_read_n((DISPLAY_MODE_ADDRESS + 2), (unsigned char *)&dir, 1);
    }

    return dir;
}
/**
 * @brief  保存LCD显示方向
 * @param  dir:LCD的显示方向
 * @note
 * @retval
 */
void bt_flash_write_display_dir(unsigned char dir)
{
    EEPROM_SectorErase((unsigned int)DISPLAY_MODE_ADDRESS);
    g_flash_flag = 0xcade;
    EEPROM_write_n((unsigned int)DISPLAY_MODE_ADDRESS, (unsigned char *)&g_flash_flag, 2);
    EEPROM_write_n((unsigned int)(DISPLAY_MODE_ADDRESS + 2), &dir, 1);
}
/**
 * @brief  保存工作模式
 * @param  mode：工作模式
 * @note
 * @retval
 */
void bt_flash_write_work_mode(unsigned short mode)
{
    EEPROM_SectorErase((unsigned int)MODE_ADDRESS);
    EEPROM_write_n((unsigned int)MODE_ADDRESS, (unsigned char *)&mode, 2);
}
/**
 * @brief  读取工作模式
 * @param  mode：返回工作模式 0-正常 1-关锁不判断弹片状态
 * @note
 * @retval
 */
void bt_flash_read_work_mode(unsigned short *mode)
{
    EEPROM_read_n(MODE_ADDRESS, (unsigned char *)mode, 2); // 获取模式
}
/**
 * @brief  保存版本号
 * @param  version:版本号
 * @note
 * @retval
 */
void bt_flash_write_version(unsigned short version)
{
    g_flash_flag = VERSION_FLAG;
    EEPROM_SectorErase((unsigned int)VERSION_FLAG_ADDRESS);
    EEPROM_write_n((unsigned int)VERSION_FLAG_ADDRESS, (unsigned char *)&g_flash_flag, 2);

    EEPROM_SectorErase((unsigned int)VERSION_ADDRESS);
    EEPROM_write_n((unsigned int)(VERSION_ADDRESS + 2), (unsigned char *)&version, 2);
}
/**
 * @brief  读取版本号
 * @param  version:返回版本号
 * @note
 * @retval
 */
void bt_flash_read_version(unsigned short *version)
{
    EEPROM_read_n(VERSION_FLAG_ADDRESS, (unsigned char *)&g_flash_flag, 2);
    if (g_flash_flag == VERSION_FLAG)
    {
        EEPROM_read_n(VERSION_ADDRESS, (unsigned char *)&version, 2);
    }
    else
    {
        *version = 0x500;
    }
}